#pragma once
#include "Main.h"
#include "Image.h"
#include "Types.h"
#include "GraphicsTypes.h"
#include "AnimationTypes.h"

struct Matrix_Stack{ // a static 656 bytes,16 aligned
Mat4x4 Stack[10];    	//10*64 bytes
Mat4x4 *Current;       //4 bytes (non dynamic)
int		Counter;      //4 bytes
int		padding[2];  //8 bytes

Matrix_Stack(){Counter =0;Current = &Stack[0];}

void Push(){
if(Counter > 8 ){Counter--;MessageBoxA ( NULL,"Mat Stack Overflow.", "Graphics Error", MB_OK );}
Stack [Counter+1] = Stack[Counter]; //copy the value into the next slot
Counter++;
Current = &Stack[Counter];
}

void Pop(){
Counter--;
Current = &Stack[Counter];
if(Counter < 0 ){Counter++;Current++;MessageBoxA ( NULL,"Mat Stack Underflow.", "Graphics Error", MB_OK );}
}

};

struct Vertex{ //20 Bytes , self explaninmatory
float x;
float y;
float z;
float u;
float v;
/*
float nx; 
float ny;
float nz;
*/
Vertex(){x=y=z=u=v=0;}
Vertex(float px,float py,float pz,float pu,float pv){
x=px;y=py;z=pz;u=pu;v=pv;}
};

struct Sprite{ //80 bytes, Renderable Image instance
GLuint h_Image; /// 4Image Texture ID
GLuint h_Verts; //  4VBO
GLuint h_Index; //  4Index Buffer Object
int    IndexSize;// 4 Amount of indecies

Mat4x4 Orientation;// 64 (memory tradeoff for fast MatMult) This is best for static objects, I'm sure they'll be a lot of em.
Sprite(GLuint hi,GLuint hv, GLuint hin,int is){h_Image =hi; h_Verts = hv; h_Index = hin; IndexSize = is;}
Sprite(Image *i,Vec3 pos = Vec3(0,0,0),Vec3 rot = Vec3(0,0,0),Vec3 scl = Vec3(1,1,1)){
Orientation = Mat4x4();
Orientation.Translate(pos.v[0],pos.v[1],pos.v[2]);
Orientation.Rotate(rot.v[0],rot.v[1],rot.v[2]);
Orientation.Scale(scl.v[0],scl.v[1],scl.v[2]);
int w_half = i->Width/2;
int h_half = i->Height/2;
Vertex vrts[] = {
Vertex(-w_half,        -h_half,0,0,0),
Vertex(i->Width-w_half,-h_half,0,1,0),
Vertex(i->Width-w_half,i->Height-h_half,0,1,1),
Vertex(-w_half,        i->Height-h_half,0,0,1)};
int idx[] = {0,1,2,0,3,2};

glGenBuffers(1,&h_Verts);
glBindBuffer(GL_ARRAY_BUFFER,h_Verts);
glBufferData(GL_ARRAY_BUFFER,4,&vrts,GL_DYNAMIC_DRAW);

glGenBuffers(1,&h_Index);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,h_Index);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,6,&idx,GL_DYNAMIC_DRAW);
IndexSize=6; h_Image = i->TexID;
}

};